#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time     : 2021/6/15 14:04
# @Author   : chengfeng
# @Email    : chengfeng@inhand.com.cn
# @File     : publicMethod.py


import sys

import yaml
import os
from datetime import datetime, timedelta


class PublicMethod:
    @staticmethod
    def empty_local_dir(local_file_dir):
        files = os.listdir(local_file_dir)
        if len(files) > 0:
            for file in os.listdir(local_file_dir):
                file_path = os.path.join(local_file_dir, file)
                if 'ovpn' in file or 'json' or 'test_result' or 'png' in file:
                    os.remove(file_path)
            print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), '本地文件夹清除成功', local_file_dir)
        else:
            print('%s此文件夹没有待清除文件' % local_file_dir)

    @staticmethod
    def get_time_stamp(delta=0, delta_type='h', time_format='%Y-%m-%dT%H:%M:%SZ'):
        """
        获取时间戳
        :param delta: 要增加或者减少的时间
        :param delta_type: 单位 d:天 h:小时 m:分钟 s:秒
        :param time_format: 显示格式
        :return:
        """
        if delta_type == 'h':
            delta_time = timedelta(hours=delta)
        elif delta_type == 'm':
            delta_time = timedelta(minutes=delta)
        elif delta_type == 's':
            delta_time = timedelta(seconds=delta)
        elif delta_type == 'd':
            delta_time = timedelta(days=delta)
        else:
            delta_time = 0
        time_stamp = datetime.utcnow() + delta_time
        return time_stamp.strftime(time_format)

    @staticmethod
    def read_yaml(file):
        if os.path.isfile(file):
            with open(file, encoding='utf-8') as f:
                yaml_info = yaml.safe_load(f.read())
            return yaml_info
        else:
            print(file, '文件不存在')
            sys.exit()


    @staticmethod
    def create_dirs(file_dir):
        """
        创建文件路径
        :param file_dir:
        :return:
        """
        if os.path.exists(file_dir):
            pass
        else:
            os.makedirs(file_dir)

if __name__ == "__main__":
    test = PublicMethod()
    print(os.path.dirname(__file__))
    os.chdir('D:/Auto_test/UI-auto-playwright//Element')
    yamlFile = test.read_yaml('login.yaml')
    print(yamlFile)